<template>
  <div class="app-container">
    <el-form ref="filterForm" :inline="true" :model="listQuery" class="filter-container">
      <el-form-item prop="id">
        <el-input v-model="listQuery.id" size="small" placeholder="ID" clearable style="width: 100px;" @keyup.enter.native="handleFilter" />
      </el-form-item>
      <el-form-item prop="acctive">
        <el-input v-model="listQuery.acctive" size="small" placeholder="系统账号" clearable style="width: 200px;" @keyup.enter.native="handleFilter" />
      </el-form-item>

      <el-form-item prop="role">
        <el-select
          v-model="listQuery.role"
          size="small"
          filterable
          clearable
          placeholder="账号角色"
          style="width: 200px"
          @change="handleFilter"
        >
          <el-option
            v-for="item in roleOptions"
            :key="item.role"
            :label="item.rolename"
            :value="item.role"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="handleFilter">筛选</el-button>
        <el-button size="small" @click="resetFilter">重置</el-button>
        <el-button
          size="small"
          type="success"
          icon="el-icon-plus"
          @click="toAccountNew"
        >添加管理员账号</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      stripe
      fit
      highlight-current-row
    >
      <el-table-column label="ID" width="80" align="center">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="系统账号">
        <template slot-scope="scope">
          {{ scope.row.acctive }}
        </template>
      </el-table-column>
      <el-table-column label="关联内部账号" align="center">
        <template slot-scope="scope">
          {{ scope.row.sub }}
        </template>
      </el-table-column>
      <el-table-column label="关联内部用户名" align="center">
        <template slot-scope="scope">
          {{ scope.row.familyName }} {{ scope.row.givenName }}
        </template>
      </el-table-column>
      <el-table-column label="账号角色" align="center">
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.role === 1"
            effect="dark"
            size="mini"
          >{{ scope.row.rolename }}</el-tag>
          <el-tag
            v-else-if="scope.row.role === 2"
            effect="dark"
            type="success"
            size="mini"
          >{{ scope.row.rolename }}</el-tag>
          <el-tag
            v-else
            effect="plain"
            type="success"
            size="mini"
          >{{ scope.row.rolename }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="180" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="toAccountEdit(scope.row.id)"
          >编辑</el-button>
          <el-button
            type="danger"
            size="mini"
            @click="removeAccount(scope.row.id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="fetchData" />
  </div>
</template>

<script>
import {
  getRolesList,
  getAccountsList,
  removeAccount
} from '@/api/account'
import Pagination from '@/components/Pagination'
import {
  formatQuery
} from '@/utils'

export default {
  name: 'AccountsList',
  components: {
    Pagination
  },
  data() {
    return {
      roleOptions: [],
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        id: formatQuery(this.$route.query.id, false),
        acctive: formatQuery(this.$route.query.acctive, false),
        role: formatQuery(this.$route.query.role, true),
        page: 1,
        size: 30
      }
    }
  },
  // watch: {
  //   $route: {
  //     handler(route) {
  //       if (!this._.isNil(route.query.page) &&
  //         !this._.isEmpty(route.query.page)
  //       ) {
  //         this.listQuery.page = parseInt(route.query.page)
  //       } else {
  //         this.listQuery.page = 1
  //       }
  //     },
  //     immediate: true
  //   }
  // },
  created() {
    if (!this._.isNil(this.$route.query.page) &&
      !this._.isEmpty(this.$route.query.page)
    ) {
      this.listQuery.page = parseInt(this.$route.query.page)
    } else {
      this.listQuery.page = 1
    }
    this.fetchRolesList()
    this.fetchData()
  },
  methods: {
    fetchRolesList() {
      getRolesList().then(response => {
        // console.log(response)
        this.roleOptions = response.data
      }).catch(err => {
        console.log(err)
      })
    },
    fetchData() {
      this.listLoading = true
      const query = {
        ...this.$route.query,
        ...this.listQuery
      }
      this.$router.push({
        query
      }).catch(err => {
        console.log(err)
      })
      getAccountsList(this.listQuery).then(response => {
        // console.log(response)
        this.list = response.data
        this.total = response.total
        this.listLoading = false
      }).catch(err => {
        console.log(err)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.list = []
      this.fetchData()
    },
    resetFilter() {
      this.$refs.filterForm.resetFields()
      for (const i in this.listQuery) {
        if (i !== 'size') {
          this.listQuery[i] = undefined
        }
      }
      // console.log(this.listQuery)
      this.$router.push({
        query: {
          page: 1
        }
      }).catch(err => {
        console.log(err)
      })
      this.handleFilter()
    },
    toAccountEdit(id) {
      this.$router.push(`/setting/accounts/edit/${id}`)
    },
    toAccountNew() {
      this.$router.push('/setting/accounts/new')
    },
    removeAccount(id) {
      this.$confirm('此操作将删除该医院, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeAccount({ id }).then(response => {
          // console.log(response)
          this.fetchData()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(err => {
          console.log(err)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
